<?php

class Azio_Posts
{

    public function __construct()
    {

    }

    public static function cut_tring_by_char($string, $max_length)
    {
        if (mb_strlen($string, "UTF-8") > $max_length) {
            $max_length = $max_length - 3;
            $string = mb_substr($string, 0, $max_length, "UTF-8");
            $pos = strrpos($string, " ");
            if ($pos === false) {
                return substr($string, 0, $max_length) . "...";
            }
            return substr($string, 0, $pos) . "...";
        } else {
            return $string;
        }
    }

    /*
     * get Category Parent
     * $taxonomy : category | timeline | section (string)
     */

    public function getCategories($taxonomy = "category")
    {
        $args = array(
            'type' => 'post',
            'parent' => 0,
            'orderby' => 'name',
            'order' => 'ASC',
            'taxonomy' => $taxonomy,
            'pad_counts' => false
        );
        $result = get_categories($args);
        return $result;
    }

    /*
     * get Category By Parent
     * $category : category id (int)
     * $taxonomy : category | timeline | section (string)
     */

    public function getCategoriesByParent($category, $taxonomy = "category")
    {

        $args = array(
            'type' => 'post',
            'child_of' => $category,
            'orderby' => 'name',
            'order' => 'ASC',
            'taxonomy' => $taxonomy
        );
        $result = get_categories($args);
        foreach ($result as &$value) {
            $value->permalink = get_term_link(intval($value->term_id), $taxonomy);
        }

        return $result;
    }

    /*
     * Get all posts
     * 
     * @param int $limit number of posts to be shown
     * @param int $category category of post
     * @param string $meta_key
     * @param string $meta_value
     * @param array $post_not_in array of posts not be shown
     * @param string $post_type type of post (post|timeline|tool)
     * @param string $orderby order posts by
     */

    public static function getPosts($limit, $category = "", $meta_key = "", $meta_value = "", $post_not_in = "", $post_type = "post", $orderby = "post_date", $order = "DESC")
    {

        $args = array(
            'posts_per_page' => $limit,
            'orderby' => $orderby,
            'order' => $order,
            'post_type' => $post_type,
            'post_status' => 'publish',
        );

        if (!empty($category)) {
            $args['category'] = $category;
        }

        if (!empty($meta_key)) {
            $args['meta_key'] = $meta_key;
            $args['meta_value'] = $meta_value;
        }

        if (!empty($post_not_in)) {
            $args['post__not_in'] = $post_not_in;
        }

        $objects = get_posts($args);

        foreach ($objects as $object) {
            $object->filter = 'sample';
            $permalink = get_permalink($object);
            $object->permalink = $permalink;

            $thumbnail_id = get_post_thumbnail_id($object->ID);
            $image = wp_get_attachment_image_src($thumbnail_id, 'thumbnail', true);
            $object->image_thumbnail = $image[0];

            $image = wp_get_attachment_image_src($thumbnail_id, 'avatar', true);
            $object->image_avatar = $image[0];

            $image = wp_get_attachment_image_src($thumbnail_id, 'medium', true);
            $object->image_medium = $image[0];

            $image = wp_get_attachment_image_src($thumbnail_id, 'large', true);
            $object->image_large = $image[0];

            if (has_excerpt($object->ID)) {
                $object->excerpt = $object->post_excerpt;
            } else {
                $object->excerpt = Azio_Posts::cut_tring_by_char(strip_tags($object->post_content), 250);
            };

            $result[] = $object;
        }

        return $result;
    }

    public static function getRelatedProducts($limit, $term_id, $post_not_in)
    {
        $args = array(
            'posts_per_page' => $limit,
            'orderby' => 'rand',
            'post_type' => 'product',
            'post_status' => 'publish',
            'post__not_in' => $post_not_in,
            'tax_query' => array(
                array(
                    'taxonomy' => 'producttype',
                    'field' => 'id',
                    'terms' => $term_id
                )
            )
        );


        $objects = get_posts($args);

        foreach ($objects as $object) {
            $object->filter = 'sample';
            $permalink = get_permalink($object);
            $object->permalink = $permalink;

            $thumbnail_id = get_post_thumbnail_id($object->ID);
            $image = wp_get_attachment_image_src($thumbnail_id, 'thumbnail', true);
            $object->image_thumbnail = $image[0];

            $image = wp_get_attachment_image_src($thumbnail_id, 'medium', true);
            $object->image_medium = $image[0];

            $image = wp_get_attachment_image_src($thumbnail_id, 'large', true);
            $object->image_large = $image[0];

            $result[] = $object;
        }

        return $result;
    }

    public static function getProducts($limit, $term_id = "", $meta_key = "", $meta_value = "", $orderby = "post_date", $order = "DESC")
    {

        $args = array(
            'posts_per_page' => $limit,
            'orderby' => $orderby,
            'order' => $order,
            'post_type' => 'product',
            'post_status' => 'publish'
        );

        if (!empty($meta_key)) {
            $args['meta_key'] = $meta_key;
            $args['meta_value'] = $meta_value;
        }

        if (!empty($term_id)) {
            $args['tax_query'] = array(
                array(
                    'taxonomy' => 'producttype',
                    'field' => 'id',
                    'terms' => $term_id
                )
            );
        }

        $objects = get_posts($args);

        foreach ($objects as $object) {
            $object->filter = 'sample';
            $permalink = get_permalink($object);
            $object->permalink = $permalink;

            $thumbnail_id = get_post_thumbnail_id($object->ID);
            $image = wp_get_attachment_image_src($thumbnail_id, 'thumbnail', true);
            $object->image_thumbnail = $image[0];

            $image = wp_get_attachment_image_src($thumbnail_id, 'medium', true);
            $object->image_medium = $image[0];

            $image = wp_get_attachment_image_src($thumbnail_id, 'large', true);
            $object->image_large = $image[0];

            $result[] = $object;
        }

        return $result;
    }

    public static function getProductField($ID)
    {
//        $result['showindex'] = get_post_meta($ID, 'showindex', true);
//        $result['featured'] = get_post_meta($ID, 'featured', true);
//        $result['gia'] = get_post_meta($ID, 'gia', true);
//        $result['donvithanhtoan'] = get_post_meta($ID, 'donvithanhtoan', true);
//        $result['donvithanhtoan'] = get_post_meta($ID, 'donvithanhtoan', true);
//        $result['thongso'] = get_post_meta($ID, 'thongso', true);
//        $result['thongsochitiet'] = get_post_meta($ID, 'thongsochitiet', false);

        global $cfs;
        $result['showindex'] = $cfs->get('showindex', $ID);
        $result['featured'] = $cfs->get('featured', $ID);
        $result['gia'] = $cfs->get('gia', $ID);
        $result['donvithanhtoan'] = $cfs->get('donvithanhtoan', $ID);
        $result['thongso'] = $cfs->get('thongso', $ID);

        return $result;
    }

    public static function getAllProducttype()
    {
        $args = array(
            'orderby' => 'id',
            'order' => 'ASC',
            'hide_empty' => true,
        );

        $result = get_terms('producttype', $args);
        return $result;
    }

}